﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Service.TraceabilityService;
using RBAC.MaHaiBo.API.DTOS;
using MediatR;
using RBAC.MaHaiBo.API.Application.Command.DeepProcessingCommand;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    /// 深加工控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeepProcessingController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<DeepProcessingController> logger;
        /// <summary>
        /// 深加工服务层
        /// </summary>
        IDeepProcessingService service;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="service">深加工服务层</param>
        /// <param name="mediator">中介者</param>
        public DeepProcessingController(ILogger<DeepProcessingController> logger, IDeepProcessingService service, IMediator mediator)
        {
            this.logger = logger;
            this.service = service;
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加深加工信息
        /// </summary>
        /// <param name="deep">要添加的深加工信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddDeepProcessing(AddDeepProcessingCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await  mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 添加深加工信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改深加工信息
        /// </summary>
        /// <param name="deep">修改后的深加工信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> updDeepProcessing(updDeepProcessingCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 修改深加工信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示深加工信息
        /// </summary>
        /// <param name="LOT">批次</param>
        /// <returns>返回深加工信息</returns>
        [HttpGet]
        public async Task<ApiResult<DeepProcessing>> DeepProcessingList([FromQuery]DeepProcessingListCommand request)
        {
            ApiResult<DeepProcessing> result = new ApiResult<DeepProcessing>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 显示深加工信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加原材信息
        /// </summary>
        /// <param name="raw">要添加原材信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddRawMaterial(AddRawMaterialCommamd request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code= await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 添加原材信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改原材信息
        /// </summary>
        /// <param name="raw">修改后的原材信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> updRawMaterial(updRawMaterialCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 修改原材信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示原料信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回原材信息</returns>
        [HttpGet]
        public async Task<ApiResult<RawMaterial>> RawMaterialList([FromQuery]RawMaterialListCommand request)
        {
            ApiResult<RawMaterial> result = new ApiResult<RawMaterial>();
            try
            {
                result.Data= await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 显示原料信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加辅料信息
        /// </summary>
        /// <param name="raw">要添加辅料信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddAccessories(AddAccessoriesCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 添加辅料信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改辅料信息
        /// </summary>
        /// <param name="raw">修改后的辅料信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> updAccessories(updAccessoriesCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 修改辅料信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示辅料信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回原材信息</returns>
        [HttpGet]
        public async Task<ApiResult<Accessories>> AccessoriesList([FromQuery]AccessoriesListCommand request)
        {
            ApiResult<Accessories> result = new ApiResult<Accessories>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 显示辅料信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加添加剂信息
        /// </summary>
        /// <param name="raw">要添加添加剂信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddAdditive(AddAdditiveCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 添加添加剂信息 出错" + ex.Message);
                throw;
            }
            return result;

        }
        /// <summary>
        /// 修改添加剂信息
        /// </summary>
        /// <param name="raw">修改后的添加剂信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> updAdditive(updAdditiveCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 修改添加剂信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示添加剂信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回添加剂信息</returns>
        [HttpGet]
        public async Task<ApiResult<Additive>> AdditiveList([FromQuery]AdditiveListCommand request)
        {
            ApiResult<Additive> result = new ApiResult<Additive>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 显示添加剂信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加包材信息
        /// </summary>
        /// <param name="raw">要添加包材信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPackingMaterials(AddPackingMaterialsCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 添加包材信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改包材信息
        /// </summary>
        /// <param name="raw">修改后的包材信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> updPackingMaterials(updPackingMaterialsCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 修改包材信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示包材信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回包材信息</returns>
        [HttpGet]
        public async Task<ApiResult<PackingMaterials>> PackingMaterialsList([FromQuery]PackingMaterialsListCommand request)
        {
            ApiResult<PackingMaterials> result = new ApiResult<PackingMaterials>();
            try
            {
                result.Data = await  mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 显示包材信息 出错" + ex.Message);
                throw;
            }
            return result;
        }


        /// <summary>
        /// 添加深加工企业信息
        /// </summary>
        /// <param name="firm">企业信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> AddDeepProcessingFirm(AddDeepProcessingFirmCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【深加工服务层】 添加深加工企业信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
